package com.one.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.one.bo.RoleIndicatorBo;
import com.one.dto.PaginationDate;
import com.one.dto.Result;
import com.one.dto.ReturnDate;
import com.one.entity.*;
import com.one.enums.IsDeletedEnum;
import com.one.enums.SexEnum;
import com.one.mapper.IndicatorInfoMapper;
import com.one.mapper.IndicatorOptionsMapper;
import com.one.mapper.RoleIndicatorMapper;
import com.one.mapper.RoleIndicatorViewMapper;
import com.one.service.RoleIndicatorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.utils.DataCheckUtils;
import com.one.utils.DateUtils;
import com.one.utils.PaginationUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 角色指标表 服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-10-10
 */
@Service
public class RoleIndicatorServiceImpl extends ServiceImpl<RoleIndicatorMapper, RoleIndicatorEntity> implements RoleIndicatorService {
    private final RoleIndicatorMapper roleIndicatorMapper;
    private final RoleIndicatorViewMapper  roleIndicatorViewMapper;
    public RoleIndicatorServiceImpl(RoleIndicatorMapper roleIndicatorMapper,RoleIndicatorViewMapper  roleIndicatorViewMapper) {
        this.roleIndicatorMapper = roleIndicatorMapper;
        this.roleIndicatorViewMapper = roleIndicatorViewMapper;
    }
    @Override
    public Result selectRoleIndicator(RoleIndicatorBo roleIndicatorBo) {
        //数据校验
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(roleIndicatorBo.getNumber(), roleIndicatorBo.getPage()), "分页参数错误!");
        int page = roleIndicatorBo.getPage();
        roleIndicatorBo.setPage(PaginationUtils.getPageIndex(roleIndicatorBo.getPage(), roleIndicatorBo.getNumber()));
        //查询项目组评分 mybatis
        List<Object> result = roleIndicatorViewMapper.selectRoleIndicator(roleIndicatorBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(roleIndicatorBo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<RoleIndicatorViewEntity> roleIndicatorViewEntities =  (List<RoleIndicatorViewEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,roleIndicatorViewEntities ));
    }

    @Override
    public Result deleteRoleIndicator(RoleIndicatorBo roleIndicatorBo) {
        QueryWrapper<RoleIndicatorEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RoleIndicatorEntity::getId,roleIndicatorBo.getId());
        int result = roleIndicatorMapper.delete(wrapper);
        if (result == 0) return Result.error().message("删除失败");
        return Result.ok();
    }

    @Override
    public Result addRoleIndicator(RoleIndicatorEntity roleIndicatorEntity) {
        roleIndicatorEntity.setCreateBy("1");
        roleIndicatorEntity.setCreateTime(DateUtils.getCurrentTime());
        //判断插入结果
        int  result = roleIndicatorMapper.insert(roleIndicatorEntity);
        if (result == 0) return Result.error().message("添加失败");
        return Result.ok();
    }

    @Override
    public Result updateRoleIndicator(RoleIndicatorEntity roleIndicatorEntity) {
        UpdateWrapper<RoleIndicatorEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(RoleIndicatorEntity::getId, roleIndicatorEntity.getId());
        int row = roleIndicatorMapper.update(roleIndicatorEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑菜单信息失败");
        }
        return Result.ok();
    }
}
